
//大厅

const log = require("../../utils/log");
const redisBase = require("../../common/RedisBase");
const game_config = require("../../config/game_config");
const {error} = require("../../utils/log");
const hallReset = require('./hallReset');

class Hall {
    constructor() {
    }

    static getInstance(){
        if(!Hall.instance){
            Hall.instance = new Hall();
            return Hall.instance;
        }else{
            return Hall.instance;
        }
    }

    //获取大厅消息返回
    async getHallRes(data,ctype,client){
        log.info('===========getHallRes=========');
        let uid = data.userId;

        let user = await redisBase.getUserInfo(uid);
        if(user){
            let ret= {
                type: ctype,
                data: {
                    isNewPlayer: user.isNewPlayer*1,
                    userInfo:{
                        userId:user.userId,
                        name:user.name,
                        avator:user.avator,
                        ballNum:user.ballNum*1,
                        ticket:user.ticket*1,
                        ranks:user.ranks*1,
                        score:user.score*1
                    },
                    gameNet:{
                        ip:game_config.game_server.host,
                        port:game_config.game_server.port
                    }
                }
            };
            client.send(JSON.stringify(ret));
        }

        //添加用户需要重置的数据
        await hallReset.addResetData(uid);

        await redisBase.closeRedis()
    }

    //获取用户的数据
    async getUserInfo(data,ctype,client){
        let uid = data.userId;

        let ret = {
            type:ctype,
            data:{
                userInfo:{},  //用户基础信息
                themeGame:{}, //主题赛游戏数据
                challengeGame:{} // 挑战赛游戏数据
            }
        }

        //step1：获取用户数据
        let user = await redisBase.getUserInfo(uid);
        if(user){
            //用户id
            ret.data.userInfo.userId = user.userId;
            //用户名
            ret.data.userInfo.name = user.name;
            //头像
            ret.data.userInfo.avator = user.avator;
            //球币数量
            ret.data.userInfo.ballNum = user.ballNum;
            //抽奖券
            ret.data.userInfo.ticket = user.ticket;
            //排名
            ret.data.userInfo.ranks = user.ranks;
            //分数，主题赛和好有pk使用到
            ret.data.userInfo.score = user.score;
        }else{
            log.error('=====getUserInfo===getUserInfo==error=')
        }

        //step2: 主题赛数据
        // this.id = `_${_uid}_${_gamType}`
        // this.uid = _uid; //用户id
        // this.name = _name; //用户名称
        // this.avator = _avator;//用户头像
        //
        // this.gamType = _gamType;    //游戏类型 1文体 2足球 3名族
        // this.dwLv = 1;      //段位等级，初始是1级
        // this.dwXzNum = 0;    //段位勋章数量，升级/降级会扣除 ，实际的勋章数量
        // this.xzNumCost = 0; //勋章数量，用来映射等级和段位等级的，升级/降级不会扣除，相当于经验
        // this.dwRank = 0;  //段位全国排名
        // this.dwType = 1;  //段位类型 [1,2,3,4,5,6,7]
        // this.countTotal = 0;  //挑战总场数
        // this.countWin = 0; //挑战胜场总数
        // this.rankZst = [0,0,0]; //知识3位图
        // this.roomId = 0; //房间号
        //
        // this.score = 0; //玩家总分数
        // this.gameStatus = 0; //0是未开始游戏 1是开始游戏
        let gameZt = await redisBase.getGameTzInfoList()

        //step3： 获取挑战赛游戏数据
        let gameTz = await redisBase.getGametzInfoByUid(uid);
        if(gameTz){

        }

        client.send(JSON.stringify(ret));
    }
}

module.exports = Hall.getInstance();